/*
 * 公用弹窗类
 * @author: huangzong
 */
const C = require('../common/common')

class Search {
    constructor() {

        // 暂时定义多个filter后期统一
        this.subjectFilter = {
            limits: 15,
            pages: 1,
            cid: '',
            search: ''
        }

        this.guestFilter = {
            pagesize: 30,
            currentpage: 1,
            search: ''
        }
        // 开卡行类型
        this.rowtype = [
            { id: 1, value: '产品' },
            { id: 2, value: '项目' },
            { id: 3, value: '卡项' }
        ]
    }

    /*
     * 搜索项目
     */
    async searchSubject(obj) {
        return await C.ajax('/shop/popup/getsubjectlist', Object.assign(this.subjectFilter, obj), 'post')
    }

    /*
     *搜索客户
     */
    async searchGuest(obj) {
        return await C.ajax('/shop/popup/getguestlist', Object.assign(this.guestFilter, obj), 'post')
    }
    /*
     * 开卡类型
     */
    searchOpenCardType(s) {
        let key = -1
        this.rowtype.forEach((row, index) => {
            if (row.id == s || row.value == s) {
                key = index
            }
        })
        return key > -1 ? [this.rowtype[key]] : this.rowtype
    }
    /*
     * 选择商品、项目，卡项
     */
    async searchTypeGoods(filter) {
        let url, tfilter
        if (filter.rowtype == 1) {
            url = '/shop/popup/getgoodslist'
            tfilter = {
                filter: JSON.stringify({
                    pagesize: 15,
                    isdisabled: 0,
                    currentpage: 1,
                    brandid: '',
                    keywords: filter.keywords
                })
            }
        } else if (filter.rowtype == 2) {
            url = '/shop/popup/getsubjectlist'
            tfilter = {
                limits: 15,
                pages: 1,
                cid: '',
                search: filter.keywords
            }
        } else {
            url = '/shop/popup/getshopcards'
            tfilter = {
                num: 1,
                lim: 15,
                sea: filter.keywords
            }
        }
        return await C.ajax(url, tfilter, 'post')
    }
    // 选择账户
    async searchAccount(filter) {

        let filters = {
            pagesize: 30,
            isdisabled: 0,
            currentpage: 1,
            keywords: ''
        }
        filter = Object.assign(filters, filter)
        return await C.ajax('/shop/popup/getaccountlist', { filter: JSON.stringify(filter) }, 'post')
    }

    // 选择员工
    async searchUser(filter) {
        let filters = {
            pagesize: 30,
            isdisabled: 0,
            currentpage: 1,
            keywords: ''
        }
        filter = Object.assign(filters, filter)
        return await C.ajax('/shop/popup/getappuser', { filter: JSON.stringify(filter), gid: filter.guestid }, 'post')
    }

    // 选择开单商品
    async searchOpenOrderGoods(filter) {
        // 根据行类型判断
        // rowtype 1 产品，2 项目，3卡耗，4 暂存
        let url, tfilter
        switch (filter.rowtype) {
            case 1:
            case 2:
                return this.searchTypeGoods(filter)
            case 3:
                url = '/shop/popup/getguestcardsubjectlist'
                tfilter = {
                    filter: JSON.stringify({
                        pagesize: 30,
                        isdisabled: 0,
                        currentpage: 1,
                        cardid: '',
                        guestid: filter.guestid,
                        keywords: filter.keywords
                    })
                }
                break
            case 4:
                url = '/shop/popup/getgueststore'
                tfilter = {
                    filter: JSON.stringify({
                        pagesize: 30,
                        isdisabled: 0,
                        currentpage: 1,
                        guestid: filter.guestid,
                        keywords: filter.keywords
                    })
                }
                break

        }
        return await C.ajax(url, tfilter, 'post')
    }
}

module.exports = new Search()